import cn.chenfei.sshd.client.common.*;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.template.Template;
import cn.hutool.extra.template.TemplateConfig;
import cn.hutool.extra.template.TemplateEngine;
import cn.hutool.extra.template.TemplateUtil;

import java.nio.charset.StandardCharsets;

/**
 * 测试将jar包部署到linux服务器
 */
public class Main {

    public static void main(String[] args) {
        SshConnect sshConnect = new SshConnect()
                .setName("root")
                .setHost("******")
                .setPort(22)
                .setPassword("******");
        SimpleSshClient simpleSshClient = new SimpleSshClient();
        boolean connect = simpleSshClient.connect(sshConnect);
        if (connect) { // ssh连接远程成功

            String inputJar = "D:\\idea_project\\git\\springboot-root\\springboot-hello\\target\\springboot-hello-2022.10.jar";
            String outputJar = "/project/" + FileUtil.getName(inputJar);
            String jdkBin = "/apps/jdk1.8/openlogic-openjdk-8u292-b10-linux-x64/bin";
            String serviceName = StrUtil.subBefore(FileUtil.getName(outputJar), ".", true);
            AppService appService = new AppService()
                    .setUser(sshConnect.getName())
                    .setDescription(serviceName)
                    .setJdkBin(jdkBin)
                    .setJarPath(outputJar);
            String inputService = "F:/" + serviceName + ".service";
            String outputService = Constant.SERVICE_PATH + FileUtil.getName(inputService);
            // 生成service文件
            TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("", TemplateConfig.ResourceMode.CLASSPATH));
            Template template = engine.getTemplate("templates/app.service");
            String result = template.render(Dict.parse(appService));
            FileUtil.writeString(result, inputService, StandardCharsets.UTF_8);

            // 使用sftp上传jar文件和.service文件
            simpleSshClient.upload(inputJar, outputJar, Console::log);
            simpleSshClient.upload(inputService, outputService, Console::log);

            // 执行命令运行项目
            CommandResult commandResult = simpleSshClient.command("systemctl daemon-reload");
            Console.log(commandResult);
            commandResult = simpleSshClient.command("systemctl start %s".formatted(serviceName));
            Console.log(commandResult);
            commandResult = simpleSshClient.command("systemctl is-active %s".formatted(serviceName));
            Console.log(commandResult);

            /*
             *
             * 重新加载systemd配置
             * systemctl daemon-reload
             * 启动myapp服务
             * systemctl start myapp
             * 检查服务是否正在运行
             * systemctl status myapp
             * 系统启动时自动启动服务
             * systemctl enable myapp
             * 系统启动时不启动服务
             * systemctl disable myapp
             * 目前有没有正在运行中。
             * systemctl is-active myapp
             * systemctl is-active myapp1 myapp2 myapp3
             * 开机时有没有默认要启用服务
             * systemctl is-enable myapp
             *
             */

            /*
             * 执行多个命令时，使用”&&“符号连接，代表前一个命令必须成功，后一个才执行。
             * 使用”||"符号，代表前一个命令必须失败，后一个才执行。
             * 使用";"符号连接多个命令，不管前面是否成功，后面的命令都会执行。
             */
            /*CommandResult commandResult = simpleSshClient.command("cd / ; ls");
            Console.log(commandResult);*/

        }
    }

}
